package cn.org.donald.controller.system;

import cn.org.donald.framework.base.BaseController;
import cn.org.donald.framework.constant.FileConstant;
import cn.org.donald.framework.constant.InfoConstant;
import cn.org.donald.framework.handler.HttpServletRequest;
import cn.org.donald.framework.handler.ServiceException;
import cn.org.donald.framework.pojo.dto.RequestInfo;
import cn.org.donald.framework.pojo.dto.UploadResult;
import cn.org.donald.framework.pojo.vo.HttpResult;
import cn.org.donald.framework.util.FileUtil;
import cn.org.donald.framework.util.RequestUtil;
import cn.org.donald.pojo.dto.system.UploadDto;
import cn.org.donald.pojo.entity.system.TableFile;
import cn.org.donald.service.system.TableFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.Objects;

/**
 * @author : Donald
 * @since : 2020/12/14 14:36
 */
@RestController
@RequestMapping("/file")
@Api(value = "文件相关接口")
public class FileController extends BaseController {

    @Reference(version = "1.0.0", group = "donald")
    private TableFileService tableFileService;

    @Autowired
    private RequestInfo requestInfo;

    @RequestMapping("/upload")
    @ApiOperation(value = "普通文件上传接口")
    public HttpResult upload(MultipartFile file, @Validated UploadDto uploadDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new ServiceException(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }

        RequestInfo requestInfo = this.requestInfo;

        Assert.notNull(file,"文件不能为空");

        String originalFileName = file.getOriginalFilename();
        UploadResult upload = FileUtil.upload(file, uploadDto.getGroupName());
        // 将文件信息存入数据库
        TableFile tableFile = new TableFile();
        tableFile.setOriginName(originalFileName);
        // 新文件名
        String url = upload.getUrl();
        String fileName = url.substring(url.lastIndexOf(FileUtil.separator));

        tableFile.setFileName(fileName);
        tableFile.setSize(file.getSize());
        tableFile.setUserId(requestInfo.getUserId());
        tableFile.setUrl(upload.getUrl());
        tableFile.setSuffixName(FileUtil.getSuffixName(file.getName()));
        tableFile.setRealPath( FileConstant.FILE_PATH_UPLOAD + uploadDto.getGroupName() + FileUtil.separator + fileName);

        boolean flat  = tableFileService.addFile(tableFile);
        return flat?toResultSuccess(InfoConstant.UPLOAD_FILE_SUCCESS,upload):toResultFail(InfoConstant.UPLOAD_FILE_FAIL);
    }
}
